using Microsoft.AspNetCore.Mvc;
using ERPBackend.Models;
using ERPBackend.Services;
using System.Collections.Generic;
using System.Threading.Tasks;
using System;

namespace ERPBackend.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class SupplierContactsController : ControllerBase
    {
        private readonly ISupplierContactService _contactService;

        public SupplierContactsController(ISupplierContactService contactService)
        {
            _contactService = contactService;
        }

        // GET: api/SupplierContacts/supplier/5
        [HttpGet("supplier/{supplierId}")]
        public async Task<ActionResult<IEnumerable<SupplierContact>>> GetContactsBySupplierId(int supplierId)
        {
            var contacts = await _contactService.GetContactsBySupplierIdAsync(supplierId);
            return Ok(contacts);
        }

        // GET: api/SupplierContacts/5
        [HttpGet("{id}")]
        public async Task<ActionResult<SupplierContact>> GetContactById(int id)
        {
            var contact = await _contactService.GetContactByIdAsync(id);

            if (contact == null)
            {
                return NotFound();
            }

            return contact;
        }

        // POST: api/SupplierContacts
        [HttpPost]
        public async Task<ActionResult<SupplierContact>> PostContact(SupplierContact contact)
        {
            try
            {
                var createdContact = await _contactService.CreateContactAsync(contact);
                return CreatedAtAction("GetContactById", new { id = createdContact.Id }, createdContact);
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        // PUT: api/SupplierContacts/5
        [HttpPut("{id}")]
        public async Task<IActionResult> PutContact(int id, SupplierContact contact)
        {
            if (id != contact.Id)
            {
                return BadRequest();
            }

            try
            {
                var updated = await _contactService.UpdateContactAsync(contact);
                if (!updated)
                {
                    return NotFound();
                }
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }

            return NoContent();
        }

        // DELETE: api/SupplierContacts/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteContact(int id)
        {
            var deleted = await _contactService.DeleteContactAsync(id);
            if (!deleted)
            {
                return NotFound();
            }

            return NoContent();
        }

        // 删除SetPrimaryContact方法，因为不再需要主要联系人功能
        // PATCH: api/SupplierContacts/supplier/5/primary/10
        // [HttpPatch("supplier/{supplierId}/primary/{contactId}")]
        // public async Task<IActionResult> SetPrimaryContact(int supplierId, int contactId)
        // {
        //     var success = await _contactService.SetPrimaryContactAsync(supplierId, contactId);
        //     if (!success)
        //     {
        //         return NotFound();
        //     }

        //     return NoContent();
        // }
    }
}